package com.leoman.utils;

import com.leoman.entity.Question;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 微信推送消息工具类
 * Created by 涂奕恒 on 2016/3/10.
 */
public class MessageTimerWork {

    private Timer timer = null;

    private String sectionId;

    private String userId;

    private Question question;

    public MessageTimerWork(String sectionId, String userId, Question question) {
        timer = new Timer();
        this.sectionId = sectionId;
        this.userId = userId;
        this.question = question;
        timer.schedule(new MyWork(), new Date());
    }

    class MyWork extends TimerTask {
        @Override
        public void run() {
            try {
                doExecute();
                timer.cancel();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /*private void doExecute() {
        System.out.println("=============================================================开始发送消息=============================================================");
        try {
            // 派发任务
            WxCpService wxCpService = (WxCpService) BeanUtil.getBean("wxCpService1");

            WxCpMessage.WxArticle wxArticle = new WxCpMessage.WxArticle();

            // 如果责任人为空，代表派发任务给部门所有人，否则只给指定责任人发送消息
            wxArticle.setTitle("派发任务");
            if (StringUtils.isNotEmpty(question.getDescriptionOne()) && question.getDescriptionOne().length() > 20) {
                wxArticle.setDescription(question.getDescriptionOne().substring(0, 20) + "...");
            } else {
                wxArticle.setDescription(question.getDescriptionOne());
            }
            wxArticle.setUrl(ConfigUtil.getString("base.url") + "weixin/info/questionInfo?type=1&id=" + question.getId());

            if (StringUtils.isNotEmpty(userId)) {
                wxCpService.messageSend(WxCpMessage.NEWS().agentId(ConfigUtil.getString("base.depart")).toUser(userId).addArticle(wxArticle).build());
            } else {
                wxCpService.messageSend(WxCpMessage.NEWS().agentId(ConfigUtil.getString("base.depart")).toUser(userId).toParty(sectionId).addArticle(wxArticle).build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("=============================================================结束发送消息=============================================================");
    }*/

    private void doExecute() {
        // 泛洲中越
        System.out.println("=============================================================开始发送消息=============================================================");
        try {
            // 派发任务
            WxCpService wxCpService = (WxCpService) BeanUtil.getBean("wxCpService");

            WxCpMessage.WxArticle wxArticle = new WxCpMessage.WxArticle();

            // 如果责任人为空，代表派发任务给部门所有人，否则只给指定责任人发送消息
            wxArticle.setTitle("派发任务");
            if (StringUtils.isNotEmpty(question.getDescriptionOne()) && question.getDescriptionOne().length() > 20) {
                wxArticle.setDescription(question.getDescriptionOne().substring(0, 20) + "...");
            } else {
                wxArticle.setDescription(question.getDescriptionOne());
            }
            wxArticle.setUrl(ConfigUtil.getString("base.url") + "weixin/info/questionInfo?type=2&id=" + question.getId());

            if (StringUtils.isNotEmpty(userId)) {
                wxCpService.messageSend(WxCpMessage.NEWS().agentId(ConfigUtil.getString("base.depart")).toUser(userId).addArticle(wxArticle).build());
            } else {
                wxCpService.messageSend(WxCpMessage.NEWS().agentId(ConfigUtil.getString("base.depart")).toUser(userId).toParty(sectionId).addArticle(wxArticle).build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("=============================================================结束发送消息=============================================================");
    }
}
